﻿package component.fileupload.action;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

import component.fileupload.exception.FileUploadException;
import component.fileupload.listener.UploadFileListener;
import component.fileupload.util.FileUtil;
import component.fileupload.vo.UploadFileVO;

public class StrutsFileUploadAction extends AbstractFileUploadAction {
	private ActionForm actionForm;
	
	public void setParameter(Map paraMap) throws FileUploadException{
		this.actionForm = (ActionForm)paraMap.get("form");
		if(actionForm == null){
			throw new FileUploadException("请设置struts上传文件的参数actionform");
		}
	}
	
	public InputStream[] getInputStreams(ServletConfig config,
			HttpServletRequest request, HttpServletResponse response)
			throws FileUploadException {
		HttpSession session = request.getSession();
		
		Boolean maxLengthExceeded = (Boolean)request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
		//超出上传的大小限制
		if (maxLengthExceeded != null && maxLengthExceeded.equals(Boolean.TRUE)){
			throw new FileUploadException(FileUtil.UPLOADFILE_MSG);
		} else {
			if(actionForm == null){
				throw new FileUploadException("请设置struts上传文件的参数actionform");
			}
			
			Hashtable files = actionForm.getMultipartRequestHandler().getFileElements();

			if (files != null) {
				fileInfoMap = new HashMap();
				
				// 创建UploadListener对象
				listener = new UploadFileListener(request.getContentLength());
				// 启动监听状态
				listener.start();
				// 将监听器对象的状态保存在Session中
				session.setAttribute("FILE_UPLOAD_STATS", listener.getState());
				
				Iterator iterator = files.keySet().iterator();
				while (iterator.hasNext()){
					String key = (String)iterator.next();
					FormFile file = (FormFile)files.get(key);
					System.out.println("上传文件的路径：" + file.getFileName());
					if (file.getFileSize() > 0 && !"".equals(file.getFileName())) {
						UploadFileVO vo = new UploadFileVO();
						
						InputStream in = null;
						try {
							in = file.getInputStream();
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							listener.error(e.getMessage());
							throw new FileUploadException(e);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							listener.error(e.getMessage());
							throw new FileUploadException(e);
						}
						vo.setInputStream(in);
						//vo.setIndex(fileInfoMap.size());
						vo.setIndex(Integer.parseInt(key.split("_")[1]));
						System.out.println("文件的顺序号：" + vo.getIndex());
						vo.setFieldName(key);
						vo.setFilePath(file.getFileName());
						vo.setFileSize(file.getFileSize());
						vo.setDestroyObj(file);//在最后才销毁该对象
						fileInfoMap.put(new Integer(fileInfoMap.size()), vo);
						
						//file.destroy();//在销毁方法里执行
					}
				}
			}
		}
		InputStream[] inArr = new InputStream[fileInfoMap.size()];
		for(int i = 0;i<fileInfoMap.size();i++){
			UploadFileVO vo = (UploadFileVO)fileInfoMap.get(new Integer(i));
			inArr[i] = vo.getInputStream();
		}
		return inArr;
	}
	
	public void destroy() {
		this.listener = null;
		
		//先销毁里面的数据
		for(int i = 0;i<fileInfoMap.size();i++){
			UploadFileVO vo = (UploadFileVO)fileInfoMap.get(new Integer(i));
			InputStream in = vo.getInputStream();
			try {
				if(in!=null) in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			((FormFile)vo.getDestroyObj()).destroy();
		}
		fileInfoMap = null;
		
	}

}
